package com.tql.pandian.controller;

import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.core.utils.CalculatedValueUtil;
import com.tql.pandian.entity.TqlStapOutChange;
import com.tql.pandian.entity.TqlStapOutChangeBomInfo;
import com.tql.pandian.service.ITqlHllShopBillFoodService;
import com.tql.pandian.util.BusinessUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/schedule")
public class TqlHllShopBillFoodController {
    @Autowired
    private ITqlHllShopBillFoodService tqlHllShopBillFoodService;

    @PostMapping("/saveStapOutChange")
    public void saveStapOutChange(@RequestParam("month") Integer month) {
        //执行  Feign
//        tqlHllShopBillFoodService.insertStapOutChange( month);


        if(0==month)
        {
            Calendar now = Calendar.getInstance();
            month = now.get(Calendar.MONTH) + 1;
        }

        //只获取已经关联的门店
        List<String> relDeptIds = tqlHllShopBillFoodService.selectRelDeptIds();
        //获取未更新到 tql_stap_out_change 表中得数据
        String shopBillFoodReportdayTableName = CommonUtil.TableName_ShopBillFoodReportDayDetail + month;
    //做过bom的food
        List<String> enableFoodCodes=tqlHllShopBillFoodService.findEnableFoodCode(null);
    //billFoodReport的数据
        List<TqlStapOutChange> entityList= tqlHllShopBillFoodService.getBillFoodReportList(shopBillFoodReportdayTableName,relDeptIds);
    //利用已经做过的bom集合enableFoodCodes过滤
        List<TqlStapOutChange> reportDayBillFoods = entityList.stream()
                .filter(entity -> enableFoodCodes.contains(entity.getFoodCode()))
                .collect(Collectors.toList());
        List<TqlStapOutChange> newEntityList = new ArrayList<>();
        if (!reportDayBillFoods.isEmpty()) {
            for (TqlStapOutChange outChange : reportDayBillFoods) {
                List<TqlStapOutChangeBomInfo> bomInfoList = tqlHllShopBillFoodService.selectShopBillFoodReportDayBomInfo(outChange.getShopId(), outChange.getFoodCode());
                if (bomInfoList.isEmpty()) {
                    continue;
                }
                for (TqlStapOutChangeBomInfo bomInfo : bomInfoList) {
                    TqlStapOutChange dto= BusinessUtil.getOutDetail(bomInfo,outChange);
                    newEntityList.add(dto);
                }
            }
            tqlHllShopBillFoodService.insertAndUpdate(month,shopBillFoodReportdayTableName,newEntityList);
        }


    }



    @PostMapping("/saveHllShopBillFoodReportDay")
    public void saveHllShopBillFoodReportDay() {
        //执行  Feign
        tqlHllShopBillFoodService.insertBillFoodReportDay();
    }

}
